package com.ruoyi.driver.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.constant.DriverConstants;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.driver.domain.TDriverInfo;
import com.ruoyi.driver.service.ITDriverInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 司机信息Controller
 * 
 * @author wzy
 * @date 2024-10-12 19:29:23
 */
@RestController
@RequestMapping("/drivers/driver")
public class TDriverInfoController extends BaseController
{
    @Autowired
    private ITDriverInfoService tDriverInfoService;

    /**
     * 查询司机信息列表
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDriverInfo tDriverInfo)
    {
        startPage();
        List<TDriverInfo> list = tDriverInfoService.selectTDriverInfoList(tDriverInfo);
        return getDataTable(list);
    }

    /**
     * 导出司机信息列表
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:export')")
    @Log(title = "司机信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDriverInfo tDriverInfo)
    {
        List<TDriverInfo> list = tDriverInfoService.selectTDriverInfoList(tDriverInfo);
        ExcelUtil<TDriverInfo> util = new ExcelUtil<TDriverInfo>(TDriverInfo.class);
        util.exportExcel(response, list, "司机信息数据");
    }

    /**
     * 获取司机信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:query')")
    @GetMapping(value = "/{driverId}")
    public AjaxResult getInfo(@PathVariable("driverId") Long driverId)
    {
        return success(tDriverInfoService.selectTDriverInfoByDriverId(driverId));
    }

    /**
     * 新增司机信息
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:add')")
    @Log(title = "司机信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDriverInfo tDriverInfo)
    {
        if (DriverConstants.NOT_UNIQUE.equals(tDriverInfoService.checkIdentityCardNumberUnique(tDriverInfo)))
        {
            return error("新增司机'" + tDriverInfo.getIdentityCardNumber() + "'失败，身份证号已存在");
        }
        if (DriverConstants.NOT_UNIQUE.equals(tDriverInfoService.checkDriverNameUnique(tDriverInfo)))
        {
            return error("新增司机'" + tDriverInfo.getDriverName() + "'失败，司机姓名已存在");
        }
        tDriverInfo.setCreateBy(getNickName());
        return toAjax(tDriverInfoService.insertTDriverInfo(tDriverInfo));
    }

    /**
     * 修改司机信息
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:edit')")
    @Log(title = "司机信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDriverInfo tDriverInfo)
    {
        if (DriverConstants.NOT_UNIQUE.equals(tDriverInfoService.checkIdentityCardNumberUnique(tDriverInfo)))
        {
            return error("修改司机'" + tDriverInfo.getIdentityCardNumber() + "'失败，身份证号已存在");
        }
        if (DriverConstants.NOT_UNIQUE.equals(tDriverInfoService.checkDriverNameUnique(tDriverInfo)))
        {
            return error("修改司机'" + tDriverInfo.getDriverName() + "'失败，司机姓名已存在");
        }
        tDriverInfo.setUpdateBy(getNickName());
        return toAjax(tDriverInfoService.updateTDriverInfo(tDriverInfo));
    }

    /**
     * 删除司机信息
     */
    @PreAuthorize("@ss.hasPermi('drivers:driver:remove')")
    @Log(title = "司机信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{driverIds}")
    public AjaxResult remove(@PathVariable Long[] driverIds)
    {
        return toAjax(tDriverInfoService.deleteTDriverInfoByDriverIds(driverIds));
    }
}
